﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConnectionLayer;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace BusinessLayer
{
    public static class PecaActions
    {
        public static bool AdicionaPeca(String RefPeca, String desig, String preco)
        {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "insert into Peca Values(@RefPeca,@desig,@preco)";
                cmd.Parameters.AddWithValue("@RefPeca", RefPeca);
                cmd.Parameters.AddWithValue("@desig", desig);
                cmd.Parameters.AddWithValue("@preco", preco);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); ; }
            }
        }

        public static bool AlteraPrecoPeca(String refPeca, String Preco) {
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "update Peca set Peca.precoP = @Preco where Peca.refP = @refPeca";
                cmd.Parameters.AddWithValue("@refPeca",refPeca);
                cmd.Parameters.AddWithValue("@Preco", Preco);
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return false; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
        }

        public static IEnumerable<String> ListaDePecas (){
            ConnectionFactory con = new ConnectionFactory();

            try
            {
                con.GetConnection.Open();
                SqlCommand cmd = con.GetConnection.CreateCommand();
                cmd.CommandText = "select * from dbo.ListarPecas";
                SqlDataReader red = cmd.ExecuteReader();
                String send= "";
                List<String> list = new List<String>();
                while(red.Read()){
                    int col = 0;
                    while (col < red.FieldCount) {
                        if (!(red.IsDBNull(col))) {
                            send += red.GetValue(col).ToString() + "     ";
                            col += 1;
                        }
                    }
                    list.Add(send);
                    send = "";
                }
                return list;
            }
            catch (Exception e) { MessageBox.Show(e.Message); return null; }
            finally
            {
                try
                {
                    con.GetConnection.Close();
                }
                catch (SqlException e) { MessageBox.Show(e.Message); }
            }
            
        }
    }
}
